Skip to content

Conversation

@SMotaal
Copy link
Contributor

@SMotaal SMotaal commented Aug 21, 2024

This will add a @ocap/test-utils/endoified helper that would make it possible to write tests that would be evaluated after @ocap/shims/endoify.

To get this working, I've made other changes that are secondary to the intent of this PR. Those changes are just for staging purposes and will be reverted once the relevant PRs land.

The approach used for testing is to first evaluate the bundled @ocap/shims/endoify source text inside a pristine JSDOM instance, which is then followed by evaluating the function body of the test itself passing to it an endowments object with the respective vitest capabilities as its only argument.

Simplified code for the endoified helper:

type VitestCapabilities = typeof import('vitest');

function endoified<Endowments extends Partial<VitestCapabilities>(
  test: (endowments: Endowments) => void,
  endowments: Endowments,
) {
  return () => {
    const evaluatorSourceText = String(test);
    const endoifySourceText = readFileSync(relativeEndoifySourcePath, 'utf-8');
    const dom = new jsdom.JSDOM('');
  
    dom.window.eval(endoifySourceText);
    (dom.window.eval(evaluatorSourceText) as typeof test)(endowments);
  };
}

Considerations

Enhancements

  • The endoified helper may need to use a different endoify bundle.

    • This would support the experimental testing efforts.
  • The endoified helper JSDOM context may need to be reused.

    • This would support the experimental testing efforts.

Requirements

  • The endoified helper may need to support the use of JS modules.

    • This would address a limitation with JSDOM that can be addressed using NODE_OPTIONS=--experimental-vm-modules along with linkage logic for vm.SourceTextModule.
  • The endoified helper maybe need to bundle the shims/dist/endoify.m?js on-demand.

    • This would address a limitation with the current test workflow:

      test:
      name: Test
      runs-on: ubuntu-latest
      needs: prepare
      strategy:
      matrix:
      node-version: [18.x, 20.x]
      package-name: ${{ fromJson(needs.prepare.outputs.child-workspace-package-names) }}
      steps:
      - uses: actions/checkout@v4
      - name: Install Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v4
      with:
      node-version: ${{ matrix.node-version }}
      - name: Install Yarn
      run: corepack enable
      - name: Restore Yarn cache
      uses: actions/setup-node@v4
      with:
      node-version: ${{ matrix.node-version }}
      cache: yarn
      - run: yarn --immutable
      - run: yarn workspace ${{ matrix.package-name }} run test
      - name: Require clean working directory
      shell: bash
      run: |
      if ! git diff --exit-code; then
      echo "Working tree dirty at end of job"
      exit 1
      fi


Alternatives


Issues

  • Inconsistencies in the behaviour of import.meta between JS modules directly in node and ts files via vitest.
  • Support for JS modules with
    JSDOM requires the

Threads

@SMotaal SMotaal closed this Sep 16, 2024
@SMotaal SMotaal deleted the smotaal/endoified-test-util branch September 16, 2024 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants